package com.langshixiaobai.game;


import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@Component
public class RoomManager {

    //存储RoomId和room
    private ConcurrentHashMap<String, Room> roomMap = new ConcurrentHashMap<>();
    //存储RoomId和User
    private ConcurrentHashMap<Integer, String> userIdRoomId = new ConcurrentHashMap<>();

    // 添加房间
    public void addRoom(Room room, Integer user1, Integer user2) {
        roomMap.put(room.getRoomId(), room);
        userIdRoomId.put(user1, room.getRoomId());
        userIdRoomId.put(user2, room.getRoomId());
    }

    //移除房间
    public void removeRoom(String roomId, int user1, int user2) {
        roomMap.remove(roomId);
        userIdRoomId.remove(user1);
        userIdRoomId.remove(user2);

    }

    //通过RoomId获取Room房间
    public Room getRoomById(String roomId) {
        return roomMap.get(roomId);
    }

    //通过userId 获取Room房间
    public Room getRoomIdByUserId(Integer userId) {
        String roomId = userIdRoomId.get(userId);
        Room room = roomMap.get(roomId);
        if (room == null) {
            return null;
        }
        return room;
    }


}
